<?php
$post=json_decode($this->input->raw_input_stream,true);
function fullpath($url)
{
    if(substr($url,0,2)=="//")
    {
        return "http:".$url;
    }
    else
    {
        return $url;
    }
}
$gid=$post["id"];
if($this->goods_model->getbytbid($gid,$shop["id"])) die("exist");

$product=array(
    "catid"=>$shop["id"],
    "tbid"=>$gid,
    "name"=>$post["name"],
    "price"=>str_replace(["US",","],"",$post["price"]),
    "source"=>"amazon",
    "source_url"=>$post["url"],
    "sales"=>$post["sales"]
);

if(!preg_match("/[\.0-9]$/i",$product["price"]))
{
    echo "error price";
    die();
}
if(empty($product["price"]) || $product["price"]<3)
{
    echo "lower price";
    die();
}
else
{
	$product["price"]=floatval($product["price"]);
}
$product["thumb"]=serialize($post["img"]);
$prop=array();
if(isset($post["colorimg"]) && $post["colorimg"]){
    $v1=array("name"=>"Color");
    $ccount=0;
    foreach($post["colorimg"] as $k=>$v)
    {   
        $v1["value"][]=$k;          
        $v1["image"][]=$v;
        $ccount+=1;
    }
    $prop["vv"][]=$v1;
}
if(isset($post["size"]) && $post["size"]){
    $v2=array("name"=>"Size");

    foreach($post["size"] as $vs)
    {   
        $v2["value"][]=$vs;  
    }
    $prop["vv"][]=$v2;
} 
$product["desc"]=contentpasswh($post["spec"]);
/*
if(isset($post["chart"]) && $post["chart"])
{
    $product["desc"]=$post["chart"].$product["desc"];
}
*/
$product["option"]=serialize($prop);
$newproduct=$this->goods_model->create($product);
if($newproduct){
    die("ok");
}else{
    die("error");
}


function contentpasswh($content)
{
    $content = preg_replace('/(?:width)=(\'|").*?\\1/', ' width="100%"', $content);
    $content = preg_replace('/(?:height)=(\'|").*?\\1/', ' ', $content);
    $content = preg_replace('/(?:max-width:\\d*\\.?\\d*(px|rem|em))/', '', $content);
    $content = preg_replace('/(?:max-height:\\d*\\.?\\d*(px|rem|em))/', '', $content);
    $content = preg_replace('/(?:min-width:\\d*\\.?\\d*(px|rem|em))/', ' ', $content);
    $content = preg_replace('/(?:min-height:\\d*\\.?\\d*(px|rem|em))/', ' ', $content);

    return $content;
}